<?php
class Crt_Walker_Nav_Menu_Edit extends Walker_Nav_Menu  {
	
	public function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
		global $_wp_nav_menu_max_depth;
		$_wp_nav_menu_max_depth = $depth > $_wp_nav_menu_max_depth ? $depth : $_wp_nav_menu_max_depth;

		$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

		ob_start();
		$item_id = esc_attr( $item->ID );
		$removed_args = array(
			'action',
			'customlink-tab',
			'edit-menu-item',
			'menu-item',
			'page-tab',
			'_wpnonce',
		);

		$original_title = '';
		if ( 'taxonomy' == $item->type ) {
			$original_title = get_term_field( 'name', $item->object_id, $item->object, 'raw' );
			if ( is_wp_error( $original_title ) )
				$original_title = false;
		} elseif ( 'post_type' == $item->type ) {
			$original_object = get_post( $item->object_id );
			$original_title = $original_object->post_title;
		}
		
		$classes = array(
			'menu-item menu-item-depth-' . $depth,
			'menu-item-' . esc_attr( $item->object ),
			'menu-item-edit-' . ( ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? 'active' : 'inactive'),
		);
		
		//$classes = empty( $item->classes ) ? array() : (array) $item->classes;
		$classes[] = 'menu-item-' . $item->ID;
		
		$title = $item->title;
		
		if ( ! empty( $item->_invalid ) ) {
			$classes[] = 'menu-item-invalid';
			/* translators: %s: title of menu item which is invalid */
			$title = sprintf( __( '%s (Invalid)','wpdance' ), $item->title );
		} elseif ( isset( $item->post_status ) && 'draft' == $item->post_status ) {
			$classes[] = 'pending';
			/* translators: %s: title of menu item in draft status */
			$title = sprintf( __('%s (Pending)','wpdance'), $item->title );
		}
	
		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';


		$title = empty( $item->label ) ? $title : $item->label;

		?>
		<li id="menu-item-<?php echo $item_id; ?>"<?php echo $class_names; ?>>
			<dl class="menu-item-bar">
				<dt class="menu-item-handle">
					<span class="item-title"><?php echo esc_html( $title ); ?></span>
					<span class="item-controls">
						<span class="item-type"><?php echo esc_html( $item->type_label ); ?></span>
						<span class="item-order hide-if-js">
							<a href="<?php
								echo wp_nonce_url(
									add_query_arg(
										array(
											'action' => 'move-up-menu-item',
											'menu-item' => $item_id,
										),
										remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
									),
									'move-menu_item'
								);
							?>" class="item-move-up"><abbr title="<?php esc_attr_e('Move up','wpdance'); ?>">&#8593;</abbr></a>
							|
							<a href="<?php
								echo wp_nonce_url(
									add_query_arg(
										array(
											'action' => 'move-down-menu-item',
											'menu-item' => $item_id,
										),
										remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
									),
									'move-menu_item'
								);
							?>" class="item-move-down"><abbr title="<?php esc_attr_e('Move down','wpdance'); ?>">&#8595;</abbr></a>
						</span>
						<a class="item-edit" id="edit-<?php echo $item_id; ?>" title="<?php esc_attr_e('Edit Menu Item','wpdance'); ?>" href="<?php
							echo ( isset( $_GET['edit-menu-item'] ) && $item_id == $_GET['edit-menu-item'] ) ? admin_url( 'nav-menus.php' ) : add_query_arg( 'edit-menu-item', $item_id, remove_query_arg( $removed_args, admin_url( 'nav-menus.php#menu-item-settings-' . $item_id ) ) );
						?>"><?php _e( 'Edit Menu Item','wpdance' ); ?></a>
					</span>
				</dt>
			</dl>

			<div class="menu-item-settings" id="menu-item-settings-<?php echo $item_id; ?>">
				<?php if( 'custom' == $item->type ) : ?>
					<p class="field-url description description-wide">
						<label for="edit-menu-item-url-<?php echo $item_id; ?>">
							<?php _e( 'URL','wpdance' ); ?><br />
							<input type="text" id="edit-menu-item-url-<?php echo $item_id; ?>" class="widefat code edit-menu-item-url" name="menu-item-url[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->url ); ?>" />
						</label>
					</p>
				<?php endif; ?>
				<p class="description description-thin">
					<label for="edit-menu-item-title-<?php echo $item_id; ?>">
						<?php _e( 'Navigation Label','wpdance' ); ?><br />
						<input type="text" id="edit-menu-item-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-title" name="menu-item-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->title ); ?>" />
					</label>
				</p>
				<p class="description description-thin">
					<label for="edit-menu-item-attr-title-<?php echo $item_id; ?>">
						<?php _e( 'Title Attribute','wpdance' ); ?><br />
						<input type="text" id="edit-menu-item-attr-title-<?php echo $item_id; ?>" class="widefat edit-menu-item-attr-title" name="menu-item-attr-title[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->post_excerpt ); ?>" />
					</label>
				</p>
				<p class="field-link-target description">
					<label for="edit-menu-item-target-<?php echo $item_id; ?>">
						<input type="checkbox" id="edit-menu-item-target-<?php echo $item_id; ?>" value="_blank" name="menu-item-target[<?php echo $item_id; ?>]"<?php checked( $item->target, '_blank' ); ?> />
						<?php _e( 'Open link in a new window/tab','wpdance' ); ?>
					</label>
				</p>
				<p class="field-css-classes description description-thin">
					<label for="edit-menu-item-classes-<?php echo $item_id; ?>">
						<?php _e( 'CSS Classes (optional)','wpdance' ); ?><br />
						<input type="text" id="edit-menu-item-classes-<?php echo $item_id; ?>" class="widefat code edit-menu-item-classes" name="menu-item-classes[<?php echo $item_id; ?>]" value="<?php echo esc_attr( implode(' ', $item->classes ) ); ?>" />
					</label>
				</p>
				<p class="field-xfn description description-thin">
					<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
						<?php _e( 'Link Relationship (XFN)','wpdance' ); ?><br />
						<input type="text" id="edit-menu-item-xfn-<?php echo $item_id; ?>" class="widefat code edit-menu-item-xfn" name="menu-item-xfn[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->xfn ); ?>" />
					</label>
				</p>
				<p class="field-description description description-wide">
					<label for="edit-menu-item-description-<?php echo $item_id; ?>">
						<?php _e( 'Description','wpdance' ); ?><br />
						<textarea id="edit-menu-item-description-<?php echo $item_id; ?>" class="widefat edit-menu-item-description" rows="3" cols="20" name="menu-item-description[<?php echo $item_id; ?>]"><?php echo esc_html( $item->description ); // textarea_escaped ?></textarea>
						<span class="description"><?php _e('The description will be displayed in the menu if the current theme supports it.','wpdance'); ?></span>
					</label>
				</p>    
				
				<?php
				/*
				 * This is the added field
				 */
				?>      
				<p class="field-megamenu-status description description-wide crt-mega-menu">
					<label for="edit-menu-item-megamenu-status-<?php echo $item_id; ?>">
						<input type="checkbox" id="edit-menu-item-megamenu-status-<?php echo $item_id; ?>" class="widefat code edit-menu-item-megamenu-status" name="menu-item-megamenu-status[<?php echo $item_id; ?>]" value="enabled" <?php checked( $item->megamenu_status, 'enabled' ); ?> />
						<strong><?php _e( 'Enable Mega Menu', 'crosstheme' ); ?></strong>
					</label>
				</p>
				<div class="crt-tab-menu-items" style="display:<?php echo $style = ($item->megamenu_status == 'enabled') ? 'block' : 'none'; ?>;">
					<ul class="crt-menu-nav nav nav-tabs" role="tablist">
						<li class="active"><a href="#general-<?php echo $item_id; ?>" aria-controls="general" role="tab" data-toggle="tab"><?php _e( 'General', 'crosstheme' ); ?></a></li>
						<li><a href="#icon-<?php echo $item_id; ?>" aria-controls="icon" role="tab" data-toggle="tab"><?php _e( 'Icon', 'crosstheme' ); ?></a></li>
						<li><a href="#background-<?php echo $item_id; ?>" aria-controls="background" role="tab" data-toggle="tab"><?php _e( 'Background', 'crosstheme' ); ?></a></li>
						<li><a href="#layout-<?php echo $item_id; ?>" aria-controls="layout" role="tab" data-toggle="tab"><?php _e( 'Layout', 'crosstheme' ); ?></a></li>
						<li><a href="#custom-content-<?php echo $item_id; ?>" aria-controls="custom" role="tab" data-toggle="tab"><?php _e( 'Custom Content', 'crosstheme' ); ?></a></li>
						<li class="last"><a href="#widget-<?php echo $item_id; ?>" aria-controls="widget" role="tab" data-toggle="tab"><?php _e( 'Widgets', 'crosstheme' ); ?></a></li>
					</ul>
					<div class="crt-menu-tabcontent tab-content">
						<div role="tabpanel" class="tab-pane active" id="general-<?php echo $item_id; ?>">
							<p class="field-megamenu-link-disable description">
								<label for="edit-menu-item-megamenu-link-disable-<?php echo $item_id; ?>">
									<input type="checkbox" id="edit-menu-item-megamenu-link-<?php echo $item_id; ?>" class="widefat code edit-menu-item-megamenu-link" name="menu-item-megamenu-link[<?php echo $item_id; ?>]" value="enabled" <?php checked( $item->megamenu_link, 'enabled' ); ?> />
									<strong><?php _e( 'Disable Link', 'crosstheme' ); ?></strong>
								</label>
							</p>
							<p class="field-megamenu-des-disable description">
								<label for="edit-menu-item-megamenu-des-disable-<?php echo $item_id; ?>">
									<input type="checkbox" id="edit-menu-item-megamenu-des-<?php echo $item_id; ?>" class="widefat code edit-menu-item-megamenu-des" name="menu-item-megamenu-des[<?php echo $item_id; ?>]" value="enabled" <?php checked( $item->megamenu_des, 'enabled' ); ?> />
									<strong><?php _e( 'Disable Description', 'crosstheme' ); ?></strong>
								</label>
							</p>
							<p class="field-scroll description">
								<label for="edit-menu-item-xfn-<?php echo $item_id; ?>">
									<?php _e( 'Scroll To','crosstheme' ); ?><br />
									<?php _e( '(Id or name of element)','crosstheme' ); ?><br />
									<input type="text" id="edit-menu-item-scroll-<?php echo $item_id; ?>" class="widethin code edit-menu-item-scroll" name="menu-item-scroll[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->scroll ); ?>" />
								</label>
							</p>
						</div>
						<div role="tabpanel" class="tab-pane" id="icon-<?php echo $item_id; ?>">
							<div class="field-icon description menu-item-actions">
								<?php
									$fa_icons = getFontAwesomeIconArray();
								?>
								<div class="crt-icon-wrapper">
									<div class="crt-icon-control">
										<div class="crt-icon-selected">
											<?php $default = esc_attr( $item->icon ); ?>
											<i class="<?php echo 'crt-icon fa '.$default ; ?>"></i>
											<span class="crt-set-icon">Set Icon</span>
										</div>
										<span class="button crt-remove-icon" title="Remove Icon"><i class="crt-icon" data-crt-icon="" >Remove Icon</i></span>
									</div>
									<div class="crt-icons">
										<div class="crt-icons-search-wrap">
											<input class="crt-icons-search" placeholder="Type to search" />
										</div>
										<?php foreach( $fa_icons as $_val => $data ): if( $_val == '' ) continue; ?>
											<span class="crt-icon-wrap" data-crt-search-terms="<?php echo strtolower( $_val ); ?>"><i class="crt-icon fa <?php echo $_val; ?>" data-crt-icon="<?php echo $_val; ?>" ></i></span>
										<?php endforeach; ?>
										<select id="edit-menu-item-icon-<?php echo $item_id; ?>" class="hidden" name="menu-item-icon[<?php echo $item_id; ?>]">
											<?php foreach( $fa_icons as $_val => $data ): ?>
											<option value="<?php echo $_val; ?>" <?php selected( $default , $_val ); ?> ><?php echo $_val; ?></option>
											<?php endforeach; ?>
										</select>
									</div>
								</div>

							</div>
						</div>
						<div role="tabpanel" class="tab-pane" id="background-<?php echo $item_id; ?>">
							<div class="field-background description">
								<div class="crt-background-wrapper">
									<div class="crt-background-item">
										<?php
											$menu_thumbnail = esc_attr( $item->background_id );
											$have_thumb = false;
											if($menu_thumbnail !==''){
												$menu_thumbnail =  wp_get_attachment_image_src( $menu_thumbnail,'full', true);
												$have_thumb = ( strlen($menu_thumbnail[0]) > 0 ? true : false );
											}
										?>
										<span class="edit-menu-item-background-wrapper">
											<?php if($have_thumb):?>
											<img src="<?php echo $menu_thumbnail[0];?>" id="background-menu-item-<?php echo $item_id; ?>" title="menu-item-<?php echo $item_id; ?>-background" alt="menu-item-<?php echo $item_id; ?>-background">
											<?php endif; ?>
										</span>
										<a class="crt_upload_background_button button" href="javascript:void(0)" style="display:<?php echo $style = ($have_thumb ? 'none' : 'inline'); ?>;">Select Background</a>
										<a class="crt_clear_background_button button" href="javascript:void(0)" style="display:<?php echo $style = ($have_thumb ? 'inline' : 'none'); ?>;">Remove Background</a>
										<input type="hidden" id="edit-menu-item-background-<?php echo $item_id; ?>" class="widefat edit-menu-item-background-id" name="menu-item-background[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->thumbnail_id ); ?>" />
									</div>
									
									<div class="crt-background-item">	
										<?php $background_repeat = esc_attr( $item->background_repeat );?>
										<?php _e( 'Background Repeat','wpdance' ); ?><br />
										<select id="edit-menu-item-background-repeat-<?php echo $item_id; ?>" class="code edit-menu-item-background-repeat" name="menu-item-background-repeat[<?php echo $item_id; ?>]">
										   <option value="repeat" <?php if($background_repeat == "repeat") echo "selected";?> >Repeat</option>
										   <option value="repeat-x" <?php if($background_repeat == "repeat-x") echo "selected";?> >Repeat X</option>
										   <option value="repeat-y" <?php if($background_repeat == "repeat-y") echo "selected";?> >Repeat Y</option>
										   <option value="no-repeat" <?php if($background_repeat == "no-repeat") echo "selected";?> >No Repeat</option>
										</select> 
									</div>	

									<div class="crt-background-item">
										<?php $background_position = esc_attr( $item->background_position );?>
										<?php _e( 'Backgroun Position','wpdance' ); ?><br />
										<select id="edit-menu-item-background-position-<?php echo $item_id; ?>" class="code edit-menu-item-background-position" name="menu-item-background-position[<?php echo $item_id; ?>]">
										   <option value="left-top" <?php if($background_position == "left-top") echo "selected";?> >Left Top</option>
										   <option value="left-bottom" <?php if($background_position == "left-bottom") echo "selected";?> >Left Bottom</option>
										   <option value="left-center" <?php if($background_position == "left-center") echo "selected";?> >Left Center</option>
										   <option value="right-top" <?php if($background_position == "right-top") echo "selected";?> >Right Top</option>
										   <option value="right-bottom" <?php if($background_position == "right-bottom") echo "selected";?> >Right Bottom</option>
										   <option value="right-center" <?php if($background_position == "right-center") echo "selected";?> >Right Center</option>
										   <option value="center-top" <?php if($background_position == "center-top") echo "selected";?> >Center Top</option>
										   <option value="center-bottom" <?php if($background_position == "center-bottom") echo "selected";?> >Center Bottom</option>
										   <option value="center-center" <?php if($background_position == "center-center") echo "selected";?> >Center Center</option>
										</select> 
									</div> 
								</div>
							</div>
						</div>
						<div role="tabpanel" class="tab-pane" id="layout-<?php echo $item_id; ?>">
							<p class="field-columns description menu-item-actions">
								<label for="edit-menu-item-wide-column-<?php echo $item_id; ?>">
									<?php $menu_column = esc_attr( $item->menu_column );?>
									<?php _e( 'Layout Columns','wpdance' ); ?><br />
									<select id="edit-menu-item-wide-column-<?php echo $item_id; ?>" class="code edit-menu-item-wide-column" name="menu-item-wide-column[<?php echo $item_id; ?>]">
										<option value="0" <?php if($menu_column == 0) echo "selected";?> >Fullwidth</option>
										<option value="2" <?php if($menu_column == 2) echo "selected";?> >2 columns</option>
										<option value="3" <?php if($menu_column == 3) echo "selected";?> >3 columns</option>
										<option value="4" <?php if($menu_column == 4) echo "selected";?> >4 columns</option>
										<option value="5" <?php if($menu_column == 5) echo "selected";?> >5 columns</option>
										<option value="6" <?php if($menu_column == 6) echo "selected";?> >6 columns</option>
									</select> 
									<Br/><Br/>
									<?php $menu_content_column = esc_attr( $item->menu_content_column );?>
									<?php _e( 'Content Columns','wpdance' ); ?><br />
									<select id="edit-menu-item-content-column-<?php echo $item_id; ?>" class="code edit-menu-item-content-column" name="menu-item-content-column[<?php echo $item_id; ?>]">
										<option value="0" <?php if($menu_content_column == 0) echo "selected";?> >Fullwidth</option>
										<option value="1" <?php if($menu_content_column == 1) echo "selected";?> >1 columns</option>
										<option value="2" <?php if($menu_content_column == 2) echo "selected";?> >2 columns</option>
										<option value="3" <?php if($menu_content_column == 3) echo "selected";?> >3 columns</option>
										<option value="4" <?php if($menu_content_column == 4) echo "selected";?> >4 columns</option>
										<option value="5" <?php if($menu_content_column == 5) echo "selected";?> >5 columns</option>
									</select> 
								</label>
							</p>
						</div>
						<div role="tabpanel" class="tab-pane" id="custom-content-<?php echo $item_id; ?>">
							<?php wp_editor( stripslashes(htmlspecialchars_decode( base64_decode($item->static_html) )), 'menu-item-static-html['.$item_id.']',array('media_buttons' => false) ); ?>
						</div>
						<div role="tabpanel" class="tab-pane" id="widget-<?php echo $item_id; ?>">
							<p class="field-wide-widget description menu-item-actions">
								<label for="edit-menu-item-sidebars-<?php echo $item_id; ?>">
									<span class="description"><?php _e('Display a Widget Area','wpdance'); ?></span><Br/>
									<?php echo crt_list_sidebars($item_id); ?>
								</label>
							</p>
						</div>
					</div>
				</div>
				<?php
				/*
				 * end added field
				 */
				?>
				<div class="menu-item-actions description-wide submitbox">
					<?php if( 'custom' != $item->type && $original_title !== false ) : ?>
						<p class="link-to-original">
							<?php printf( __('Original: %s','wpdance'), '<a href="' . esc_attr( $item->url ) . '">' . esc_html( $original_title ) . '</a>' ); ?>
						</p>
					<?php endif; ?>
					<a class="item-delete submitdelete deletion" id="delete-<?php echo $item_id; ?>" href="<?php
					echo wp_nonce_url(
						add_query_arg(
							array(
								'action' => 'delete-menu-item',
								'menu-item' => $item_id,
							),
							remove_query_arg($removed_args, admin_url( 'nav-menus.php' ) )
						),
						'delete-menu_item_' . $item_id
					); ?>"><?php _e('Remove','wpdance'); ?></a> <span class="meta-sep"> | </span> <a class="item-cancel submitcancel" id="cancel-<?php echo $item_id; ?>" href="<?php echo esc_url( add_query_arg( array('edit-menu-item' => $item_id, 'cancel' => time()), remove_query_arg( $removed_args, admin_url( 'nav-menus.php' ) ) ) );
						?>#menu-item-settings-<?php echo $item_id; ?>"><?php _e('Cancel','wpdance'); ?></a>
				</div>

				<input class="menu-item-data-db-id" type="hidden" name="menu-item-db-id[<?php echo $item_id; ?>]" value="<?php echo $item_id; ?>" />
				<input class="menu-item-data-object-id" type="hidden" name="menu-item-object-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object_id ); ?>" />
				<input class="menu-item-data-object" type="hidden" name="menu-item-object[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->object ); ?>" />
				<input class="menu-item-data-parent-id" type="hidden" name="menu-item-parent-id[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_item_parent ); ?>" />
				<input class="menu-item-data-position" type="hidden" name="menu-item-position[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->menu_order ); ?>" />
				<input class="menu-item-data-type" type="hidden" name="menu-item-type[<?php echo $item_id; ?>]" value="<?php echo esc_attr( $item->type ); ?>" />
			</div><!-- .menu-item-settings-->
			<ul class="menu-item-transport"></ul>
		<?php
		$output .= ob_get_clean();
    }
	
}
?>
